<?php
	App::import('Sanitize');
	
	class AppController extends Controller {
		var $helpers = array(
			'Html',
			'Form',
			'Javascript',
			'Session',
			'Text',
			'Number',
			'Time',
			'Js',
		
			// Plasa
			'Layout'
		);
		
		var $components = array(
			'Cookie',
			'Session',
			'RequestHandler',
			'Security',
		
			// Plasa
			'Plasa'
		);
		
		var $plugins = array(
			//'Facebook.Facebook',
			//'Twitter'
		);
		
		var $uses = array(
			'Shop'
		);
		
		var $shopInfo = array();
		var $userInfo = array();
		
		var $publicAction = array();
		
		var $Shop; // Model
		
		var $domain;
		
		public function beforeFilter() {
			parent::beforeFilter();
			Security::setHash('sha1');
			
			if (!$this->Session->check('User')) {
				if (!in_array($this->params['action'], $this->publicAction)) {
					//$this->redirect(Configure::read('Site.Url.main').'/login');
				}
			}
			
			if (!empty($this->params['layout'])) {
				$this->layout = $this->params['layout'];
			}
			
			if ($this->RequestHandler->isAjax()) {
				header('Content-type: application/x-javascript');
				Configure::write('debug', 0);
				$this->layout = 'ajax';
			}
		}
		
		public function beforeRender() {
			parent::beforeRender();
			
			if (!empty($this->shopInfo)) {
				if (!empty($this->pageTitle)) {
					$this->pageTitle = $this->pageTitle . ' - ' . $this->shopInfo['Shop']['name'];
				}
				else {
					$this->pageTitle = $this->shopInfo['Shop']['name'];
				}
				
				if ($this->shopInfo['Shop']['description']) {
					Configure::write('Site.description', $this->shopInfo['Shop']['description']);
				}
				if ($this->shopInfo['Shop']['keywords']) {
					Configure::write('Site.description', $this->shopInfo['Shop']['keywords']);
				}
			}
			
			# fixed to CakePHP 1.3.6
			if (!empty($this->pageTitle)) {
				$this->set('title_for_layout', $this->pageTitle);
			}
			
			# load javascript
			/*
			$jscript = array(
				'default' => array(
					'jquery'			=> Configure::read('Site.Url.static') . '/js/jquery-1.7.1.min.js',
					'bootstrap'			=> Configure::read('Site.Url.static') . '/js/bootstrap.min.js'
				)
				//'qtip'				=> Configure::read('Site.Url.static') . '/js/plugin/jquery.qtip.1.0.0.rc3.min.js',
				//'fancybox'			=> Configure::read('Site.Url.static') . '/js/plugin/jquery.fancybox.1.3.1.pack.js',
				//'fileuploader'		=> Configure::read('Site.Url.static') . '/js/fileuploader.js',
				//'tinymce'			=> Configure::read('Site.Url.static') . '/js/tiny_mce/tiny_mce.js'
			);
			
			$jscript['default'][] = $jscript['jquery'];
			$jscript['default'][] = Configure::read('Site.Url.static') . '/js/commons.js';
			*/
			# load css
			/*
			$cssheet = array(
				//'fancybox'			=> Configure::read('Site.Url.static') . '/css/fancybox.css',
				//'fileuploader'		=> Configure::read('Site.Url.static') . '/css/fileuploader.css',
				'default' => array(
					Configure::read('Site.Url.static') . '/css/base.css'
				)
			);
			*/
			
			$jscript = Configure::read('Site.Js.default');
			
			$cssheet = Configure::read('Site.Css.default');
			
			# dashboard
			if (!empty($this->params['dashboard'])) {
				$cssheet['dashboard'] = Configure::read('Site.Url.static') . '/css/dashboard.css';
				//$cssheet['default-ie6'][] = Configure::read('Site.Url.static') . '/css/dashboard-ie.css';
			}
			# shop
			else {
				$cssheet['style'] = 'style.css';
				//$cssheet['default'][] = Configure::read('Site.Url.static') . '/css/shop.css';
				//$cssheet['default-ie6'][] = Configure::read('Site.Url.static') . '/css/shop-ie.css';
			}
			
			# print views
			if (!empty($this->params['layout']) && $this->params['layout'] == 'print') {
				$this->viewPath .= DS . 'print';
				$cssheet = Configure::read('Site.Css.default');
				$cssheet['print'] = Configure::read('Site.Url.static') . '/css/print.css'; 
			}
			
			$this->set(compact('jscript', 'cssheet'));
		}
		
		private function __loadConfig() {
			return $this->shopInfo['Configuration'];
		}
	}
